package action.adapter;

import java.util.ArrayList;

import action.newelifile.AddressActivity;
import action.newelifile.R;
import action.pojo.LocalFilePojo;
import action.util.LoadSetting;
import action.util.Tool;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.provider.MediaStore.Video.Thumbnails;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FavoriteFileAdapter extends BaseAdapter{
	LayoutInflater layoutInflater;
	int layoutId;
	ArrayList<LocalFilePojo> array;
	ViewHolder holder;
	Context context;

	public FavoriteFileAdapter(Context context, int textViewResourceId, ArrayList<LocalFilePojo> array) {
		this.context = context;
		this.layoutId = textViewResourceId;
		this.array = array; 
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {   
		final LocalFilePojo files = array.get(position);
		if(convertView==null){
			holder = new ViewHolder();
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=layoutInflater.inflate(layoutId, parent,false);

			holder.iv = (ImageView) convertView.findViewById(R.id.historyIcon);
			holder.tv = (TextView) convertView.findViewById(R.id.historyText);
			holder.path = (TextView) convertView.findViewById(R.id.historyPath);

			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}

		if(files!=null){
			if(files.getName()!=null){
				Bitmap bitmap = null;
				Drawable icon = null;
				BitmapFactory.Options options=new BitmapFactory.Options();
				options.inSampleSize = 8;
				if(files.getIsFolder()==0){
					icon = LoadSetting.loadFolder((AddressActivity)context);
				}else{

					if (Tool.checkExtendFile(files.getName(), ".txt")
							|| Tool.checkExtendFile(files.getName(), ".csv")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.text_file);
					} else if (Tool.checkExtendFile(files.getName(), ".xml")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.xml_file);
					} else if (Tool.checkExtendFile(files.getName(), ".flv")
							|| Tool.checkExtendFile(files.getName(), ".3gp")
							|| Tool.checkExtendFile(files.getName(), ".avi")) {
						bitmap = ThumbnailUtils.createVideoThumbnail(
								files.getPath(), Thumbnails.MICRO_KIND);
						icon = new BitmapDrawable(bitmap);
					} else if (Tool.checkExtendFile(files.getName(), ".mp3")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.mp3_file);
					} else if (Tool.checkExtendFile(files.getName(), ".doc")
							|| Tool.checkExtendFile(files.getName(), ".docx")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.word_file);
					} else if (Tool.checkExtendFile(files.getName(), ".ppt")
							|| Tool.checkExtendFile(files.getName(), ".pptx")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.pptx_file);
					} else if (Tool.checkExtendFile(files.getName(), ".xls")
							|| Tool.checkExtendFile(files.getName(), ".xlsx")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.xlsx_file);
					} else if (Tool.checkExtendFile(files.getName(), ".zip")
							|| Tool.checkExtendFile(files.getName(), ".rar")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.rar_file);
					} else if (Tool.checkExtendFile(files.getName(), ".jpg")
							|| Tool.checkExtendFile(files.getName(), ".jpeg")
							|| Tool.checkExtendFile(files.getName(), ".png")
							|| Tool.checkExtendFile(files.getName(), ".bmp")
							|| Tool.checkExtendFile(files.getName(), ".gif")) {
						bitmap = BitmapFactory.decodeFile(files.getPath(),options);
						icon = new BitmapDrawable(bitmap);
					} else if (Tool.checkExtendFile(files.getName(), ".apk")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.apk_file);
					} else if (Tool.checkExtendFile(files.getName(), ".exe")) {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.exe_file);
					} else {
						icon = ((AddressActivity)context).getResources().getDrawable(
								R.drawable.unknown_file);
					}
				}
				files.setIcon(icon);
				holder.iv.setImageDrawable(icon);
				if (files.getName()!=null)
					holder.tv.setText(files.getName());
				if (files.getPath()!=null) 
					holder.path.setText(files.getPath());
			}
		}

		return convertView;
	}

	public static class ViewHolder{
		ImageView iv;
		TextView tv,path;
	}

	@Override
	public int getCount() {
		return array.size();
	}
	@Override
	public Object getItem(int position) {
		return array.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
}
